########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script runs the regression analyses
#             for the correspondence analysis 
# Author:     Jan Schwalbach (21/07/2022)
########################################################

library(quanteda)
library(stargazer)
library(jtools)
library(sjPlot)
theme_set(theme_sjplot())
theme_sjplot(base_size = 16, base_family = "Times")
library(extrafont)
font_import()
loadfonts(device="win") 

# Load all annotated data sets and annotate them
# with respective legislative period information

load(file="Positions_NL_CA.Rdata")
Positions_NL_1 <- Positions_NL
Positions_NL_1$period <- 1
Positions_NL_1$minority <- 0
Positions_NL_1$RRP_size <- 6
Positions_NL_1$support <- 0

load(file="Positions_NL_CA_10.Rdata")
Positions_NL_2 <- Positions_NL
Positions_NL_2$period <- 2
Positions_NL_2$minority <- 1
Positions_NL_2$RRP_size <- 16
Positions_NL_2$support <- 1

load(file="Positions_NL_CA_12.Rdata")
Positions_NL_3 <- Positions_NL
Positions_NL_3$period <- 3
Positions_NL_3$minority <- 0
Positions_NL_3$RRP_size <- 10
Positions_NL_3$support <- 0

load(file="Positions_DK_CA.Rdata")
Positions_DK_1 <- Positions_DK
Positions_DK_1$period <- 1
Positions_DK_1$minority <- 1
Positions_DK_1$RRP_size <- 7.3
Positions_DK_1$support <- 0

load(file="Positions_DK_CA_01.Rdata")
Positions_DK_2 <- Positions_DK
Positions_DK_2$period <- 2
Positions_DK_2$minority <- 1
Positions_DK_2$RRP_size <- 12.3
Positions_DK_2$support <- 1

load(file="Positions_DK_CA_05.Rdata")
Positions_DK_3 <- Positions_DK
Positions_DK_3$period <- 3
Positions_DK_3$minority <- 1
Positions_DK_3$RRP_size <- 13.4
Positions_DK_3$support <- 1

load(file="Positions_SW_CA.Rdata")
Positions_SW_1 <- Positions_SW
Positions_SW_1$period <- 1
Positions_SW_1$minority <- 1
Positions_SW_1$RRP_size <- 5.7
Positions_SW_1$support <- 0

load(file="Positions_SW_CA_14.Rdata")
Positions_SW_2 <- Positions_SW
Positions_SW_2$period <- 1
Positions_SW_2$minority <- 1
Positions_SW_2$RRP_size <- 14
Positions_SW_2$support <- 0

load(file="Positions_D_CA.Rdata")
Positions_D_1 <- Positions_D
Positions_D_1$period <- 1
Positions_D_1$minority <- 0
Positions_D_1$RRP_size <- 13.3
Positions_D_1$support <- 0

#Bind all data sets together and delete positions of the PRRPs (in order to not compare them to themselves)

Positions_all <- rbind(Positions_D_1,Positions_DK_1,Positions_DK_2,Positions_DK_3,Positions_NL_1,Positions_NL_2,Positions_NL_3,Positions_SW_1,Positions_SW_2)

Positions_all <- Positions_all[Positions_all$Party != "DF",]
Positions_all <- Positions_all[Positions_all$Party != "AfD",]
Positions_all <- Positions_all[Positions_all$Party != "SD",]
Positions_all <- Positions_all[Positions_all$Party != "PVV",]

# Run the main model

main_model <- lm(Difference ~ left + Government + matter + period + minority  + support*Government + RRP_size + type + country, data=Positions_all)
summary(main_model)

# Coding the GAL-TAN variable for the GAL-TAN Model

Positions_all$gal <- 0       
Positions_all$gal[Positions_all$Party == "EL"] <- 1
Positions_all$gal[Positions_all$Party == "RV"] <- 1
Positions_all$gal[Positions_all$Party == "SOC"] <- 1
Positions_all$gal[Positions_all$Party == "SF"] <- 1

Positions_all$gal[Positions_all$Party == "FDP"] <- 1
Positions_all$gal[Positions_all$Party == "GREENS"] <- 1
Positions_all$gal[Positions_all$Party == "LEFT"] <- 1
Positions_all$gal[Positions_all$Party == "SPD"] <- 1

Positions_all$gal[Positions_all$Party == "D66"] <- 1
Positions_all$gal[Positions_all$Party == "GL"] <- 1
Positions_all$gal[Positions_all$Party == "PvdA"] <- 1

Positions_all$gal[Positions_all$Party == "C"] <- 1
Positions_all$gal[Positions_all$Party == "L"] <- 1
Positions_all$gal[Positions_all$Party == "MP"] <- 1
Positions_all$gal[Positions_all$Party == "S"] <- 1
Positions_all$gal[Positions_all$Party == "V"] <- 1

# Run the GAL-TAN model

main_model_gal <- lm(Difference ~ gal + Government + matter + period + minority  + support*Government + RRP_size + type + country, data=Positions_all)
summary(main_model_gal)

##### Load data with no preprocessing

load(file="Positions_NL_CA_NO.Rdata")
Positions_NL_1 <- Positions_NL
Positions_NL_1$period <- 1
Positions_NL_1$minority <- 0
Positions_NL_1$RRP_size <- 6
Positions_NL_1$support <- 0

load(file="Positions_NL_CA_10_NO.Rdata")
Positions_NL_2 <- Positions_NL
Positions_NL_2$period <- 2
Positions_NL_2$minority <- 1
Positions_NL_2$RRP_size <- 16
Positions_NL_2$support <- 1

load(file="Positions_NL_CA_12_NO.Rdata")
Positions_NL_3 <- Positions_NL
Positions_NL_3$period <- 3
Positions_NL_3$minority <- 0
Positions_NL_3$RRP_size <- 10
Positions_NL_3$support <- 0

load(file="Positions_DK_CA_NO.Rdata")
Positions_DK_1 <- Positions_DK
Positions_DK_1$period <- 1
Positions_DK_1$minority <- 1
Positions_DK_1$RRP_size <- 7.3
Positions_DK_1$support <- 0

load(file="Positions_DK_CA_01_NO.Rdata")
Positions_DK_2 <- Positions_DK
Positions_DK_2$period <- 2
Positions_DK_2$minority <- 1
Positions_DK_2$RRP_size <- 12.3
Positions_DK_2$support <- 1

load(file="Positions_DK_CA_05_NO.Rdata")
Positions_DK_3 <- Positions_DK
Positions_DK_3$period <- 3
Positions_DK_3$minority <- 1
Positions_DK_3$RRP_size <- 13.4
Positions_DK_3$support <- 1

load(file="Positions_SW_CA_NO.Rdata")
Positions_SW_1 <- Positions_SW
Positions_SW_1$period <- 1
Positions_SW_1$minority <- 1
Positions_SW_1$RRP_size <- 5.7
Positions_SW_1$support <- 0

load(file="Positions_SW_CA_14_NO.Rdata")
Positions_SW_2 <- Positions_SW
Positions_SW_2$period <- 1
Positions_SW_2$minority <- 1
Positions_SW_2$RRP_size <- 14
Positions_SW_2$support <- 0

load(file="Positions_D_CA_NO.Rdata")
Positions_D_1 <- Positions_D
Positions_D_1$period <- 1
Positions_D_1$minority <- 0
Positions_D_1$RRP_size <- 13.3
Positions_D_1$support <- 0

#Bind all data sets together and delete positions of the PRRPs (in order to not compare them to themselves)

Positions_all <- rbind(Positions_D_1,Positions_DK_1,Positions_DK_2,Positions_DK_3,Positions_NL_1,Positions_NL_2,Positions_NL_3,Positions_SW_1,Positions_SW_2)

Positions_all <- Positions_all[Positions_all$Party != "DF",]
Positions_all <- Positions_all[Positions_all$Party != "AfD",]
Positions_all <- Positions_all[Positions_all$Party != "SD",]
Positions_all <- Positions_all[Positions_all$Party != "PVV",]

# Run the model without preprocessing

no_prepro_model <- lm(Difference ~ left + Government + matter + period + minority  + support*Government + RRP_size + type + country, data=Positions_all)
summary(no_prepro_model)


##### Load data with normalized scores

load(file="Positions_NL_CA_normal.Rdata")
Positions_NL_1 <- Positions_NL
Positions_NL_1$period <- 1
Positions_NL_1$minority <- 0
Positions_NL_1$RRP_size <- 6
Positions_NL_1$support <- 0

load(file="Positions_NL_CA_10_normal.Rdata")
Positions_NL_2 <- Positions_NL
Positions_NL_2$period <- 2
Positions_NL_2$minority <- 1
Positions_NL_2$RRP_size <- 16
Positions_NL_2$support <- 1

load(file="Positions_NL_CA_12_normal.Rdata")
Positions_NL_3 <- Positions_NL
Positions_NL_3$period <- 3
Positions_NL_3$minority <- 0
Positions_NL_3$RRP_size <- 10
Positions_NL_3$support <- 0

load(file="Positions_DK_CA_normal.Rdata")
Positions_DK_1 <- Positions_DK
Positions_DK_1$period <- 1
Positions_DK_1$minority <- 1
Positions_DK_1$RRP_size <- 7.3
Positions_DK_1$support <- 0

load(file="Positions_DK_CA_01_normal.Rdata")
Positions_DK_2 <- Positions_DK
Positions_DK_2$period <- 2
Positions_DK_2$minority <- 1
Positions_DK_2$RRP_size <- 12.3
Positions_DK_2$support <- 1

load(file="Positions_DK_CA_05_normal.Rdata")
Positions_DK_3 <- Positions_DK
Positions_DK_3$period <- 3
Positions_DK_3$minority <- 1
Positions_DK_3$RRP_size <- 13.4
Positions_DK_3$support <- 1

load(file="Positions_SW_CA_normal.Rdata")
Positions_SW_1 <- Positions_SW
Positions_SW_1$period <- 1
Positions_SW_1$minority <- 1
Positions_SW_1$RRP_size <- 5.7
Positions_SW_1$support <- 0

load(file="Positions_SW_CA_14_normal.Rdata")
Positions_SW_2 <- Positions_SW
Positions_SW_2$period <- 1
Positions_SW_2$minority <- 1
Positions_SW_2$RRP_size <- 14
Positions_SW_2$support <- 0

load(file="Positions_D_CA_normal.Rdata")
Positions_D_1 <- Positions_D
Positions_D_1$period <- 1
Positions_D_1$minority <- 0
Positions_D_1$RRP_size <- 13.3
Positions_D_1$support <- 0

#Bind all data sets together and delete positions of the PRRPs (in order to not compare them to themselves)

Positions_all <- rbind(Positions_D_1,Positions_DK_1,Positions_DK_2,Positions_DK_3,Positions_NL_1,Positions_NL_2,Positions_NL_3,Positions_SW_1,Positions_SW_2)

Positions_all <- Positions_all[Positions_all$Party != "DF",]
Positions_all <- Positions_all[Positions_all$Party != "AfD",]
Positions_all <- Positions_all[Positions_all$Party != "SD",]
Positions_all <- Positions_all[Positions_all$Party != "PVV",]

# Run the model with normalized scores

normalized_model <- lm(Difference ~ left + Government + matter + period + minority  + support*Government + RRP_size + type + country, data=Positions_all)
summary(normalized_model)

# Subset data set for first terms only and run the first term model

Positions_all_firstterm <- Positions_all[Positions_all$period == 1,]
main_model_first <- lm(Difference ~ left + Government + matter + RRP_size + type+ country, data=Positions_all_firstterm)
summary(main_model_first)

# Reproduction of Figure 1

plot_model(main_model,type = "est", title = "", axis.title = "Estimates", vline.color = "black", colors = "Set1", axis.labels = c("Opposition*PRRP Support","Sweden","Netherlands","Denmark"
                                                                                                                                  ,"Opposition Debate","Government Debate","PRRP Size"
                                                                                                                                  ,"PRRP Support","Minority","Term Number","Immigration Debate"
                                                                                                                                  ,"Education Debate","Opposition Party","Left Party"))+theme_sjplot(base_size = 20, base_family = "TT Times New Roman")

# Text output for all models

stargazer(main_model,main_model,main_model,main_model,main_model_gal,type = "text")
